﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace BatRunner
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Bat Runner -- Powered by Heroius");
            //Console.WriteLine("Batrunner commands will be activated with higher priority. You can press '>' at the beginning of commands to force app pass the following content to input stream.");
            while (true)
            {
                Console.WriteLine("Give a usable bat safe name (without extension):");
                var input = Console.ReadLine().ToUpper();
                //commands

                var inpbat = string.Format("{0}/{1}.bat", Environment.CurrentDirectory, input);

                StreamWriter InputWriter = null;
                if (File.Exists(inpbat))
                {
                    bool exited = false;
                    Process target = new Process();
                    target.StartInfo.FileName = inpbat;
                    target.StartInfo.CreateNoWindow = true;
                    target.StartInfo.UseShellExecute = false;
                    target.StartInfo.RedirectStandardInput = true;
                    target.StartInfo.RedirectStandardOutput = true;
                    target.StartInfo.RedirectStandardError = true;
                    target.OutputDataReceived += (ss, ee) =>
                    {
                        if (!string.IsNullOrEmpty(ee.Data))
                        {
                            Console.WriteLine(ee.Data);
                        }
                    };
                    target.ErrorDataReceived += (ss, ee) =>
                    {
                        if (!string.IsNullOrEmpty(ee.Data))
                        {
                            Console.WriteLine(ee.Data);
                        }
                    };
                    target.EnableRaisingEvents = true;
                    target.Exited += (ss, ee) =>
                    {
                        target.WaitForExit();//make sure the process has really exited
                        InputWriter.Close();
                        if (target.HasExited)//make sure again
                        {
                            Console.WriteLine(string.Format("Exit code: {0}", target.ExitCode));
                            exited = true;
                        }
                    };

                    target.Start();
                    InputWriter = target.StandardInput;
                    target.BeginOutputReadLine();
                    target.BeginErrorReadLine();
                    while (!exited)
                    {
                        try
                        {
                            InputWriter.Write(Convert.ToChar(Console.Read()));
                            Thread.Sleep(100);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(string.Format("Process error:{0}", ex.Message));
                            break;
                        }
                    }
                }
                else Console.WriteLine("Target doesn't exist!");
            }
        }
    }
}
